﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace img2edp
{
    public partial class Form1 : Form
    {
        private string imageName;
        public Form1()
        {
            InitializeComponent();
        }

        private void openFileBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.openFileDialog1.Filter = "Image(*.bmp)|*.bmp|Image(*.jpg)|*.jpg";
                this.openFileDialog1.InitialDirectory = Application.StartupPath + "\\img\\1.bmp";
                if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
                    return;
                this.button_SavePicture.Enabled = true;
                this.imageName = this.openFileDialog1.FileName;
                this.pictureBox1.Image = Image.FromFile(this.imageName);
                Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
                int width = bitmap.Width;
                int height = bitmap.Height;
                this.GetPictureDataRed(this.imageName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public byte[] GetPictureDataRed(string imagePath)
        {
            Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
            int width = bitmap.Width;
            int num1 = bitmap.Height;
            byte[] pictureDataRed1 = new byte[1000000];
            int index1 = 0;
            for (int y = 0; y <= num1 - 1; ++y)
            {
                byte num9 = 0;
                for (int index5 = 0; index5 <= width / 8 - 1; ++index5)
                {
                    for (int index6 = 0; index6 < 8; ++index6)
                    {
                        byte num10 = (byte)((uint)num9 * 2U);
                        Color pixel = bitmap.GetPixel(index5 * 8 + index6, y);
                        if (pixel.R > (byte)100 && pixel.G <= (byte)100 && pixel.B <= (byte)100)
                        {
                            num9 = num10;
                        }
                        else
                            num9 = (byte)((uint)num10 + 1U);
                    }
                    int num11 = width != 800 || num1 != 480 ? (width != 1304 ? 1 : (num1 != 984 ? 1 : 0)) : 0;
                    pictureDataRed1[index1] = (byte)(num11 != 0 ? num9 : ~num9);
                    ++index1;
                }
            }
            return pictureDataRed1;
        }

        public byte[] GetPictureData(string imagePath)
        {
            Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
            int width = bitmap.Width;
            int height = bitmap.Height;
            byte[] pictureData1 = new byte[1000000];
            int index1 = 0;
            for (int y = 0; y <= height - 1; ++y)
            {
                byte num8 = 0;
                for (int index5 = 0; index5 <= width / 8 - 1; ++index5)
                {
                    for (int index6 = 0; index6 < 8; ++index6)
                    {
                        byte num9 = (byte)((uint)num8 * 2U);
                        Color pixel = bitmap.GetPixel(index5 * 8 + index6, y);
                        num8 = pixel.R > (byte)10 || pixel.G > (byte)10 || pixel.B > (byte)10 ? (byte)((uint)num9 + 1U) : num9;
                    }
                    int num10 = width != 800 || height != 480 ? (width != 1304 ? 1 : (height != 984 ? 1 : 0)) : 0;
                    pictureData1[index1] = (byte)(num10 != 0 ? num8 : ~num8);
                    ++index1;
                }
            }
            return pictureData1;
        }

        private void SaveArray_Click(object sender, EventArgs e)
        {
            byte[] numArray1 = new byte[10000000];
            byte[] numArray2 = new byte[10000000];
            string str1 = Application.StartupPath + "\\img\\1.bmp";
            string[] strArray1 = new string[10000000];
            string[] strArray2 = new string[10000000];
            Bitmap bitmap = new Bitmap(this.pictureBox1.Image);
            int width = bitmap.Width;
            int num1 = bitmap.Height;
            this.saveFileDialog1.InitialDirectory = Application.StartupPath + "\\img\\bitmap.c";
            this.saveFileDialog1.Filter = "bitmap(*.c)|*.c";
            this.saveFileDialog1.FilterIndex = 1;
            this.saveFileDialog1.RestoreDirectory = true;
            this.saveFileDialog1.AddExtension = true;
            this.saveFileDialog1.Title = "请保存图片";
            this.saveFileDialog1.FileName = "1";
            if (this.saveFileDialog1.ShowDialog() != DialogResult.OK || this.pictureBox1.Image == null)
                return;

            numArray1 = this.GetPictureData(this.imageName);
            numArray2 = this.GetPictureDataRed(this.imageName);
            int num2 = width * num1 / 8;

            int index1 = 0;
            int index2 = 0;
            while (index1 < num2)
            {
                strArray1[index2] = "0x" + Convert.ToString(numArray1[index1], 16);
                strArray2[index2] = "0x" + Convert.ToString(numArray2[index1], 16);
                strArray1[index2] = this.DataToAll(strArray1[index2]);
                strArray2[index2] = this.DataToAll(strArray2[index2]);
                int index3 = index2 + 1;
                strArray1[index3] = ",";
                strArray2[index3] = ",";
                if ((index3 / 2 + 1) % 16 == 0)
                {
                    ++index3;
                    strArray1[index3] = "\r\n";
                    strArray2[index3] = "\r\n";
                }
                ++index1;
                index2 = index3 + 1;
            }
            int count = index2;
            string str2 = string.Join("", strArray1, 0, count);
            string str3 = string.Join("", strArray2, 0, count);
            File.WriteAllText(this.saveFileDialog1.FileName, "const unsigned char gImage_BW1[" + num2.ToString() + "]={/*black and white picture*/\r\n" + str2.ToUpper() + "\r\n};\r\n\r\nconst unsigned char gImage_R1[" + num2.ToString() + "]={/*red and white picture*/\r\n" + str3.ToUpper() + "\r\n};\r\n", Encoding.ASCII);
            Process.Start("NotePad.exe", this.saveFileDialog1.FileName);
        }

        public string DataToAll(string num)
        {
            switch (num)
            {
                case "0x0":
                    num = "0x00";
                    break;
                case "0x1":
                    num = "0x01";
                    break;
                case "0x2":
                    num = "0x02";
                    break;
                case "0x3":
                    num = "0x03";
                    break;
                case "0x4":
                    num = "0x04";
                    break;
                case "0x5":
                    num = "0x05";
                    break;
                case "0x6":
                    num = "0x06";
                    break;
                case "0x7":
                    num = "0x07";
                    break;
                case "0x8":
                    num = "0x08";
                    break;
                case "0x9":
                    num = "0x09";
                    break;
                case "0xa":
                    num = "0x0a";
                    break;
                case "0xb":
                    num = "0x0b";
                    break;
                case "0xc":
                    num = "0x0c";
                    break;
                case "0xd":
                    num = "0x0d";
                    break;
                case "0xe":
                    num = "0x0e";
                    break;
                case "0xf":
                    num = "0x0f";
                    break;
            }
            return num;
        }
    }
}
